import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { BadRequestException, ValidationPipe } from '@nestjs/common';
import * as Result from 'src/tool/result';
import { flattenDeep } from 'lodash';
import { GlobalExceptionsFilter } from './common/global-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      forbidNonWhitelisted: true,
      exceptionFactory: (errors) => {
        const error = flattenDeep(
          errors.map((err) => Object.values(err.constraints)),
        );
        const result = Result.error(Result.Code.ARGUMENTS_ERROR, error);
        return new BadRequestException(result);
      },
    }),
  );
  app.useGlobalFilters(new GlobalExceptionsFilter());
  await app.listen(3000);
}
bootstrap();
